/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Reflection;

namespace MyPhotoIndex.Utilities
{
    public class ApplicationFolders
    {
        private const string PHOTO_INDEX_FILE_NAME = "MyPhotoIndex.mdb";
        private const string UpgraderInstallationFile = "MyPhotoIndexUpdate.exe";
        private const string UpgraderMonitorFile = "MyPhotoIndex.Updater.exe";
        private const string UpgraderMonitorRunningFile = "MyPhotoIndex.UpdaterInstance.exe";

        static string m_alternateDefaultIndexFolder;
        static string m_centralStorageLocation = String.Empty;
        static string m_cahcePath;

        private ApplicationFolders()
        {

        }

        public static bool IsAppPortable
        {
            get
            {
               return (string.IsNullOrEmpty(CommandLineArguments.GetArgumentValue("Portable")) == false);
            }
        }

        public static bool UseCentralStorage
        {
            get
            {
                return IsAppPortable;
            }
        }

        public static string ApplicationFolder
        {
            get
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                return Path.GetDirectoryName(asm.Location);
            }
        }

        public static string MyPhotoIndexUserFolder
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyPhotoIndex");
            }
        }

        public static string PluginsFolder
        {
            get
            {
                return Path.Combine(ApplicationFolder, "Plugins");
            }
        }

        private static string ms_providersFolderOverrride;

        public static void SetProvidersFolder(string path)
        {
            ms_providersFolderOverrride = path;
        }

        public static string ProvidersFolder
        {
            get
            {
                if (string.IsNullOrEmpty(ms_providersFolderOverrride) == false)
                {
                    return ms_providersFolderOverrride;
                }

                return Path.Combine(ApplicationFolder, "Providers");
            }
        }

        public static string DefaultIndexFolder
        {
            get
            {
                if (string.IsNullOrEmpty(m_alternateDefaultIndexFolder) == false)
                {
                    return m_alternateDefaultIndexFolder;
                }
                else
                {
                    string appDataPath = CommandLineArguments.GetArgumentValue("adp");
                    if (string.IsNullOrEmpty(appDataPath) == false)
                    {
                        m_alternateDefaultIndexFolder = appDataPath.Trim();
                        return m_alternateDefaultIndexFolder;
                    }
                }

                return Path.Combine(MyPhotoIndexUserFolder, "Data");
            }
        }

        public static string DefaultConfigurationPath
        {
            get
            {
                if (IsAppPortable == false)
                {
                    return MyPhotoIndexUserFolder;
                }

                return Path.Combine(DefaultIndexFolder, "Config");
            }
        }

        public static string DefaultLogPath
        {
            get
            {
                if (IsAppPortable == false)
                {
                    return MyPhotoIndexUserFolder;
                }

                return Path.Combine(DefaultIndexFolder, "Log");
            }
        }

        public static string CachePath
        {
            get
            {
                if (IsAppPortable == true)
                {
                    return Path.Combine(DefaultIndexFolder, "Cache");
                }

                if (string.IsNullOrEmpty(m_cahcePath) == true)
                {
                    return Path.Combine(MyPhotoIndexUserFolder, "Cache");
                }

                return m_cahcePath;
            }
            set { m_cahcePath = value; }
        }

        public static string CentralStorageLocation
        {
            get
            {
                if (IsAppPortable == false)
                {
                    return m_centralStorageLocation;
                }

                return CommandLineArguments.GetArgumentValue("csl");
            }
        }

        public static string DefaultIndexFileName
        {
            get
            {
                return PHOTO_INDEX_FILE_NAME;
            }
        }

        public static string ApplicationUpgradeInstallerName
        {
            get { return UpgraderInstallationFile; }
        }

        public static string UpgraderMonitorFileName
        {
            get { return UpgraderMonitorFile; }
        }

        public static string UpgraderRuningMonitorFileName
        {
            get { return UpgraderMonitorRunningFile; }
        }
    }
}
